package com.chen.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.constant.LkdConstant;
import com.chen.constant.MessageConstant;
import com.chen.exception.BaseException;
import com.chen.mapper.PartnerMapper;
import com.chen.pojo.common.PageResult;
import com.chen.pojo.dto.PartnerDto;
import com.chen.pojo.entry.Partner;
import com.chen.pojo.vo.PartnerVo;
import com.chen.service.PartnerService;
import com.chen.utils.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author chen
 * @version 1.0
 * @create_date 2025/8/17
 * 合作商管理业务实现类
 */
@Service
@Transactional
public class PartnerServiceImpl implements PartnerService {

    @Autowired
    private PartnerMapper partnerMapper;

    /**
     * 分页查询
     *
     * @param pageIndex 起始页
     * @param pageSize  页大小
     * @param name      合作商名称
     * @return 查询结果
     */
    @Override
    public PageResult<Partner> searchList(Integer pageIndex, Integer pageSize, String name) {
        Page<Partner> page = new Page<>(pageIndex, pageSize);
        partnerMapper.selectPageForPartner(page, name);
        return PageUtils.mapperResult(page);
    }

    /**
     * 新增合作商
     *
     * @param partnerDto 合作商基本信息
     * @return 结果
     */
    @Override
    public Boolean addPartner(PartnerDto partnerDto) {
        Partner partner = new Partner();
        BeanUtils.copyProperties(partnerDto, partner);
        partner.setCreateTime(LocalDateTime.now());
        partner.setUpdateTime(LocalDateTime.now());
        partnerMapper.insert(partner);
        return true;
    }

    /**
     * 根据id 查询合作商
     *
     * @param id 合作商id
     * @return 查询结果
     */
    @Override
    public PartnerVo getPartnerById(Integer id) {
        Partner partner = partnerMapper.selectById(id);
        return PartnerVo.builder()
                .id(partner.getId())
                .name(partner.getName())
                .account(partner.getAccount())
                .vmCount(partner.getVmCount())
                .ratio(partner.getRatio())
                .contact(partner.getContact())
                .phone(partner.getPhone())
                .mobile(partner.getMobile())
                .build();
    }

    /**
     * 更新合作商信息
     *
     * @param id         合作商id
     * @param partnerDto 合作商信息
     * @return 更新结果
     */
    @Override
    public Boolean updatePartner(Integer id, PartnerDto partnerDto) {
        Partner partner = new Partner();
        BeanUtils.copyProperties(partnerDto, partner);
        partner.setUpdateTime(LocalDateTime.now());
        partner.setId(id);
        partnerMapper.updateById(partner);
        return true;
    }

    /**
     * 删除合作商的信息
     *
     * @param id 合作商id
     * @return result
     */
    @Override
    public Boolean deletePartner(Integer id) {
        Partner partner = new Partner();
        partner.setId(id);
        // 判断当前合作商是否存在点位或者机器
        List<Integer> partnerIds = partnerMapper.selectFromNodeAndMachine(id);
        if (!CollectionUtils.isEmpty(partnerIds)) {
            throw new BaseException(MessageConstant.PARTNER_NOT_DELETE);
        }
        partnerMapper.deleteById(partner);
        return true;
    }

    /**
     * 重置密码
     *
     * @param id 合作商id
     * @return
     */
    @Override
    public Boolean updatePassword(Integer id) {
        Partner partner = new Partner();
        partner.setUpdateTime(LocalDateTime.now());
        partner.setPassword(LkdConstant.DEFAULT_PASSWORD);
        partner.setId(id);
        partnerMapper.updateById(partner);
        return true;
    }

    /**
     * 统计合作商数量
     *
     * @return 合作商数量
     */
    @Override
    public Long getPartnerCount() {
        return partnerMapper.selectCount(null);
    }
}
